﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Codelines
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btConvert_Click(object sender, EventArgs e)
        {
            if (txtSentence.Text == null || txtSentence.Text == "") return;
            Func<List<string>, string, bool> IsClassic = (list, s) =>
            {
                foreach (var l in list)
                {
                    if (s.Contains(l)) return true;
                }
                return false;
            };
            Func<string, string> SingleBlank = x => { while (x.Contains("\r\n\r\n")) { x = x.Replace("\r\n\r\n", "\r\n"); } return x; };
            var txtSent = txtSentence.Text.Replace("\t", "").Replace("    ", " ").Replace(";", ";\r\n");
            var listNeedReplace = new List<string> { "|111", "|222", "|333", "|444", "|555", "|666", "|777", "|888", "|999" };
            var resultString = "";
            if (IsClassic(listNeedReplace, txtSent))
            {
                var list1 = txtVariable_1.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list2 = txtVariable_2.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list3 = txtVariable_3.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list4 = txtVariable_4.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list5 = txtVariable_5.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list6 = txtVariable_6.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list7 = txtVariable_7.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list8 = txtVariable_8.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list9 = txtVariable_9.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var listOfList = new List<string[]>();
                listOfList.Add(list1);
                listOfList.Add(list2);
                listOfList.Add(list3);
                listOfList.Add(list4);
                listOfList.Add(list5);
                listOfList.Add(list6);
                listOfList.Add(list7);
                listOfList.Add(list8);
                listOfList.Add(list9);
                Func<List<string[]>, int> GetMaxIndex = list =>
                {
                    var max = list[0].Count();
                    foreach (var l in list)
                    {
                        if (l.Count() != 0 && l.Count() != max) { return 0; }
                        max = Math.Max(l.Count(), max);
                    }
                    return max;
                };
                var maxIndex = GetMaxIndex(listOfList);
                if (maxIndex == 0) { txtResult.Text = "There is something wrong in variable lines!"; txtResult.ForeColor = Color.Red; return; }
                int headNum = 0;
                int step = 1;
                try
                {
                    headNum = Convert.ToInt32(txtHeadNum.Text);
                    step = Convert.ToInt32(txtSteps.Text);
                }
                catch
                {
                    txtResult.ForeColor = Color.Red;
                    txtResult.Text = "There is something wrong with the parameters!";
                    return;
                }
                for (int i = 0; i < maxIndex; i++)
                {
                    resultString += txtSent.Replace("***", "" + (i * step + headNum))
                        .Replace("|111", list1.Count() > i ? list1[i] : "")
                        .Replace("|222", list2.Count() > i ? list2[i] : "")
                        .Replace("|333", list3.Count() > i ? list3[i] : "")
                        .Replace("|444", list4.Count() > i ? list4[i] : "")
                        .Replace("|555", list5.Count() > i ? list5[i] : "")
                        .Replace("|666", list6.Count() > i ? list6[i] : "")
                        .Replace("|777", list7.Count() > i ? list7[i] : "")
                        .Replace("|888", list8.Count() > i ? list8[i] : "")
                        .Replace("|999", list9.Count() > i ? list9[i] : "") + "\r\n";
                }
                txtResult.ForeColor = Color.Black;
                txtResult.Text = SingleBlank(resultString);
                return;
            }
            else
            {
                if (txtSent.Contains("***"))
                {
                    int headNum = 0;
                    int step = 1;
                    int tailNum = 9;
                    try
                    {
                        headNum = Convert.ToInt32(txtHeadNum.Text);
                        step = Convert.ToInt32(txtSteps.Text);
                        if (txtTailNum.Text != "null") { tailNum = Convert.ToInt32(txtTailNum.Text); }
                    }
                    catch
                    {
                        txtResult.ForeColor = Color.Red;
                        txtResult.Text = "There is something wrong with the parameters!";
                        return;
                    }
                    var steps = tailNum - headNum + 1;
                    for (int i = 0; i < steps; i++)
                    {
                        txtResult.ForeColor = Color.Black;
                        resultString += txtSent.Replace("***", "" + (i * step + headNum)) + "\r\n";
                    }
                    txtResult.Text = SingleBlank(resultString);
                }
                else
                {
                    txtResult.ForeColor = Color.Red;
                    txtResult.Text = "Do nothing because nothing is read!";
                    return;
                }
            }
        }

        private void btCopyto_Click(object sender, EventArgs e)
        {
            var t = txtResult.Text + "" == "" ? " " : txtResult.Text;
            Clipboard.SetText(t);
        }

        private void btConvertCopy_Click(object sender, EventArgs e)
        {
            btConvert_Click(sender, e);
            btCopyto_Click(sender, e);
        }
    }
}

